
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>品牌列表案例</title>
  <link rel="stylesheet" href="../lib/bootstrap.css">
  <link rel="stylesheet" href="../css/brandlist.css">
</head>

<body>

  <div id="app">
    <!-- 卡片区域 -->
    <div class="card">
      <div class="card-header">
        添加品牌
      </div>
      <div class="card-body">
        <!-- 添加品牌的表单区域 -->
        <form>
          <div class="form-row align-items-center">
            <div class="col-auto">
              <div class="input-group mb-2">
                <div class="input-group-prepend">
                  <div class="input-group-text">品牌名称</div>
                </div>
                <input type="text" class="form-control" placeholder="请输入品牌名称" v-model="brand">
              </div>
            </div>
            <div class="col-auto">
              <button type="submit" class="btn btn-primary mb-2" @click.prevent="add">添加</button>
            </div>
          </div>
        </form>
      </div>
    </div>

    <!-- 表格区域 -->
    <table class="table table-bordered table-hover table-striped">
      <thead>
        <tr>
          <th scope="col">#</th>
          <th scope="col">品牌名称</th>
          <th scope="col">状态</th>
          <th scope="col">创建时间</th>
          <th scope="col">操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(car,index) in carList" :key="index">
          <td>{{car.id}}</td>
          <td>{{car.carBrand}}</td>
          <td>
            <div class="custom-control custom-switch">
              <input type="checkbox" class="custom-control-input" :id="car.id" :checked="car.isState" @click="changeState(index)">
              <label  class="custom-control-label"  :for="car.id">{{car.text}}</label>
            </div>
          </td>
          <td>{{car.time}}</td>
          <td>
            <a href="javascript:;" @click="deleteCar(car.id)">删除</a>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
  <script src="vue.js"></script>
  <script>
      new Vue({
        el:'#app',
        data:{
          carList:[
            {
              id:1,
              carBrand:'宝马',
              isState:true,
              text:'已启用',
              time:'Mon Nov 15 2021 17:40:50 GMT+0800(中国标准时间)'
            },
            {
              id:2,
              carBrand:'奔驰',
              isState:false,
              text:'已禁用',
              time:'Mon Nov 15 2021 17:40:50 GMT+0800(中国标准时间)'
            },
            {
              id:3,
              carBrand:'奥迪',
              isState:true,
              text:'已启用',
              time:'Mon Nov 15 2021 17:40:50 GMT+0800(中国标准时间)'
            }
          ],
          brand:''
        },
        methods: {
          changeState(index){
            this.carList[index].isState=!this.carList[index].isState
            this.carList[index].text=this.carList[index].isState==true?'已启用':'已禁用'
          },
          deleteCar(id){
            this.carList=this.carList.filter(item => item.id!=id)
          },
          add(){
            this.carList.push({
              id:this.carList.length+1,
              carBrand:this.brand,
              isState:true,
              text:'已启用',
              time:'Mon Nov 15 2021 17:40:50 GMT+0800(中国标准时间)'
            })
          }
        },
      })
  </script>
</body>

</html>